package com.school.service.Impl;

import com.school.common.ServiceResultEnum;
import com.school.common.TeaShopCategoryLevelEnum;
import com.school.common.TeaShopOrderStatusEnum;
import com.school.controller.vo.OrderItemVO;
import com.school.entity.Order;
import com.school.entity.OrderItem;
import com.school.mapper.OrderItemMapper;
import com.school.mapper.OrderMapper;
import com.school.service.OrderService;
import com.school.util.BeanUtil;
import com.school.util.PageQueryUtil;
import com.school.util.PageResult;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;


@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderMapper orderMapper;
    @Resource
    private OrderItemMapper orderItemMapper;

    @Override
    public PageResult getOrdersPage(PageQueryUtil pageQueryUtil) {
        List<Order> list = orderMapper.findOrderList(pageQueryUtil);
        int totalCount = orderMapper.getTotalOrders(pageQueryUtil);
        PageResult pageResult = new PageResult(list, totalCount, pageQueryUtil.getLimit(), pageQueryUtil.getPage());
        return pageResult;
    }

   @Override
public String updateOrderInfo(Order order) {
    Order temp = orderMapper.selectByPrimaryKey(order.getOrderId());
    if (temp != null && temp.getOrderStatus() >= 0 && temp.getOrderStatus() < 4) {
        temp.setTotalPrice(order.getTotalPrice());
        temp.setUserAddress(order.getUserAddress());
        temp.setUpdateTime(new Date());
        if (orderMapper.updateByPrimaryKeySelective(temp) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }
    return ServiceResultEnum.ORDER_NOT_EXIST_ERROR.getResult();
}

    @Override
    public String checkDone(Long[] ids) {
        List<Order> orders = orderMapper.selectByPrimaryKeys(Arrays.asList(ids));
        StringBuffer errorOrderNos = new StringBuffer();

        if (orders.size() > 0) {
            for (Order order : orders) {
                if (order.getIsDeleted() == 1) {
                    errorOrderNos.append(order.getOrderNo()).append(" ");
                    continue;
                }
                if (order.getOrderStatus() != 1) {
                    errorOrderNos.append(order.getOrderNo()).append(" ");
                }
            }

            if (errorOrderNos.length() > 0) {
                return ServiceResultEnum.ORDER_STATUS_ERROR.getResult() + ":" + errorOrderNos;
            } else {
                int row = orderMapper.checkDone(Arrays.asList(ids));
                // if (row > 0) {
                //     return ServiceResultEnum.SUCCESS.getResult();
                // } else {
                //     return ServiceResultEnum.DB_ERROR.getResult();
                // }
                return row > 0 ? ServiceResultEnum.SUCCESS.getResult() : ServiceResultEnum.DB_ERROR.getResult();
            }
        }

        return ServiceResultEnum.DATA_NOT_EXIST.getResult();
    }

    @Override
    public String checkOut(Long[] ids) {
        List<Order> orders = orderMapper.selectByPrimaryKeys(Arrays.asList(ids));
        StringBuffer errorOrderNos = new StringBuffer();
        if (orders.size() > 0) {
            for (Order order : orders) {
                if (order.getIsDeleted() == 1) {
                    errorOrderNos.append(order.getOrderNo()).append(" ");
                    continue;
                }
                if (order.getOrderStatus() != 2) {
                    errorOrderNos.append(order.getOrderNo()).append(" ");
                }
            }
            if (errorOrderNos.length() > 0) {
                return ServiceResultEnum.ORDER_STATUS_ERROR.getResult() + ":" + errorOrderNos;

            } else {
                int row = orderMapper.checkOut(Arrays.asList(ids));
                return row > 0 ? ServiceResultEnum.SUCCESS.getResult() : ServiceResultEnum.DB_ERROR.getResult();
            }
        }
        return ServiceResultEnum.DATA_NOT_EXIST.getResult();
    }


    @Override
    public String closeOrder(Long[] ids) {
        List<Order> orders = orderMapper.selectByPrimaryKeys(Arrays.asList(ids));
        StringBuffer errorOrderNos = new StringBuffer();
        if (orders.size() > 0) {
            for (Order order : orders) {
                if (order.getIsDeleted() == 1) {
                    errorOrderNos.append(order.getOrderNo()).append(" ");
                    continue;
                }
                if (order.getOrderStatus() ==4 || order.getOrderStatus() <=0) {
                    errorOrderNos.append(order.getOrderNo()).append(" ");
                }
            }
            if (errorOrderNos.length() > 0) {
                return ServiceResultEnum.ORDER_STATUS_ERROR.getResult() + ":" + errorOrderNos;

            } else {
                int row = orderMapper.checkOrder(Arrays.asList(ids), TeaShopOrderStatusEnum.ORDER_CLOSED_BY_JUDGE.getOrderStatus());
                return row > 0 ? ServiceResultEnum.SUCCESS.getResult() : ServiceResultEnum.DB_ERROR.getResult();

            }
        }
        return ServiceResultEnum.DATA_NOT_EXIST.getResult();
    }

    @Override
    public List<OrderItemVO> getOrderItemsByOrderId(Long orderId) {
        Order order = orderMapper.selectByPrimaryKey(orderId);
        if (order != null) {
            List<OrderItem> orderItems = orderItemMapper.selectByOrderId(orderId);
            List<OrderItemVO> orderItemVOS = BeanUtil.copyList(orderItems, OrderItemVO.class);
            return orderItemVOS;
        }
        return null;
    }

}